﻿using MyNeighbourhood.Data;
using MyNeighbourhood.Enums;
using MyNeighbourhood.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyNeighbourhood.Managers;
using MyNeighbourhood.Models;
using System.IO;

namespace MyNeighbourhood.Controllers
{
    public class EventsController : BaseController
    {
        private EventsManager eventsManager = new EventsManager();
        private static int participantIndex = 0;

        public PartialViewResult AddParticipant(int index)
        {
            if (participantIndex < index)
            {
                participantIndex = index;
            }

            var model = new ParticipantModel { Index = participantIndex++ };
            return PartialView("_AddParticipant", model);
        }       

        //
        // GET: /Events/

        public ActionResult Index()
        {
            var model = this.GetAllEventsModel();
            return View(model);
        }

        //
        // GET: /Events/Details/5

        public PartialViewResult Details(int id)
        {
            var eventDb = eventsManager.GetEventById(id);
            var model = new EventModel();
            model.AdditionalInfo = eventDb.AdditionalInfo;
            model.Email = eventDb.Email;
            model.IsPublic = eventDb.IsPublic;
            model.Name = eventDb.Name;
            model.Phone = eventDb.Phone;
            model.Presentation = eventDb.Presentation;
            model.Time = eventDb.Time;
            model.EventType = (EventType)eventDb.Type;
            model.Website = eventDb.Website;
            model.Date = eventDb.Date;
            model.OtherTargets = eventDb.OtherTargetTypes;
            model.Location = new LocationModel { Street = eventDb.Location.Street, Number = eventDb.Location.Number };
            foreach (var eventParticipant in eventDb.EventParticipants)
            {
                model.Participants.Add(new ParticipantModel { Name = eventParticipant.Participant.Name, Photo = eventParticipant.Participant.Photo, Programme = eventParticipant.Participant.Programme });
            }

            foreach (var target in eventDb.EventTargets)
            {
                model.Target.Add(target.TargetType);
            }

            return PartialView("_Details", model);
        }

        //
        // GET: /Events/Create

        public ActionResult Create()
        {
            ViewBag.EventTypes = new SelectList(Enum.GetValues(typeof(EventType)));

            var targetTypes = eventsManager.GetDefaultTargetTypes();

            EventModel model = new EventModel();
            model.Participants.Add(new ParticipantModel { });
            model.TargetCheck = new List<TargetCheck>();
            foreach (var type in targetTypes)
            {
                model.TargetCheck.Add(new TargetCheck { TargetTypeId = type.TargetTypeId });
            }

            ViewBag.TargetTypes = targetTypes;
            participantIndex = 0;

            return View(model);
        }

        //
        // POST: /Events/Create

        [HttpPost]
        public ActionResult Create(EventModel model)
        {
            if (model.Day == 0 || model.Month == 0 || model.Year == 0)
            {
                ModelState.AddModelError("Date", "The Date field is required.");
            }
            else
            {
                try
                {
                    model.Date = new DateTime(model.Year, model.Month, model.Day);
                }
                catch (Exception)
                {
                    ModelState.AddModelError("Date", "The Date is invalid.");
                }
            }

            if (ModelState.IsValid)
            {
                model.Target = new List<TargetType>();
                foreach (var target in model.TargetCheck)
                {
                    if (target.IsChecked)
                    {
                        model.Target.Add(eventsManager.GetTargetTypeById(target.TargetTypeId));
                    }
                }

                if (eventsManager.CreateOrUpdateEvent(model, Request.Files) != null)
                {
                    return RedirectToAction("Index");
                }
            }

            ViewBag.EventTypes = new SelectList(Enum.GetValues(typeof(EventType)));
            var targetTypes = eventsManager.GetDefaultTargetTypes();
            ViewBag.TargetTypes = targetTypes;
            return View(model);
        }

        //
        // GET: /Events/Edit/5

        public ActionResult Edit(int id)
        {
            var eventDb = eventsManager.GetEventById(id);
            var model = new EventModel();
            model.EventId = eventDb.EventId;
            model.AdditionalInfo = eventDb.AdditionalInfo;
            model.Email = eventDb.Email;
            model.IsPublic = eventDb.IsPublic;
            model.Name = eventDb.Name;
            model.Phone = eventDb.Phone;
            model.Presentation = eventDb.Presentation;
            model.Time = eventDb.Time;
            model.EventType = (EventType)eventDb.Type;
            model.Website = eventDb.Website;
            model.Date = eventDb.Date;
            model.Day = eventDb.Date.Value.Day;
            model.Month = eventDb.Date.Value.Month;
            model.Year = eventDb.Date.Value.Year;
            model.Location = new LocationModel { Street = eventDb.Location.Street, Number = eventDb.Location.Number };
            model.OtherTargets = eventDb.OtherTargetTypes;

            foreach (var eventParticipant in eventDb.EventParticipants)
            {
                model.Participants.Add(new ParticipantModel { Name = eventParticipant.Participant.Name, Photo = eventParticipant.Participant.Photo, Programme = eventParticipant.Participant.Programme });
            }
            model.Participants.Add(new ParticipantModel { });

            foreach (var target in eventDb.EventTargets)
            {
                model.Target.Add(target.TargetType);
            }

            ViewBag.EventTypes = new SelectList(Enum.GetValues(typeof(EventType)));
            var targetTypes = eventsManager.GetDefaultTargetTypes();
            model.TargetCheck = new List<TargetCheck>();
            foreach (var type in targetTypes)
            {
                model.TargetCheck.Add(new TargetCheck { TargetTypeId = type.TargetTypeId, IsChecked = eventsManager.IsTargetChecked(type.TargetTypeId, id) });
            }

            ViewBag.TargetTypes = targetTypes;
            participantIndex = 0;

            return View(model);
        }

        //
        // POST: /Events/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, EventModel model)
        {
            try
            {
                model.Date = new DateTime(model.Year, model.Month, model.Day);
            }
            catch (Exception)
            {
                ModelState.AddModelError("Date", "The Date is invalid.");
            }

            if (ModelState.IsValid)
            {
                model.Target = new List<TargetType>();
                foreach (var target in model.TargetCheck)
                {
                    if (target.IsChecked)
                    {
                        model.Target.Add(eventsManager.GetTargetTypeById(target.TargetTypeId));
                    }
                }

                if (eventsManager.CreateOrUpdateEvent(model, Request.Files) != null)
                {
                    return RedirectToAction("Index");
                }
            }

            ViewBag.EventTypes = new SelectList(Enum.GetValues(typeof(EventType)));
            var targetTypes = eventsManager.GetDefaultTargetTypes();
            ViewBag.TargetTypes = targetTypes;
            return View(model);
        }

        //
        // GET: /Events/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Events/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
